package com.hyx.nettystudy;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.net.InetSocketAddress;

/**
 * @ClassName HttpServer
 * @Description TODO
 * @Author 黄乙轩
 * @Date 2021/11/17 16:54
 * @Version 1.0
 **/

public class HttpServer {
    int port;

    public HttpServer(int port){
        this.port = port;
    }

    public void start() throws InterruptedException {
        ServerBootstrap bootstrap = new ServerBootstrap();
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup work = new NioEventLoopGroup();
        bootstrap.group(boss,work)
                .handler(new LoggingHandler(LogLevel.DEBUG))
                .channel(NioServerSocketChannel.class)
                .childHandler(new HttpServerInitializer());

        ChannelFuture future = bootstrap.bind(new InetSocketAddress(port)).sync();
        System.out.println("server start up on port: " + port);
        future.channel().closeFuture().sync();
    }

    public static void main(String[] args) throws InterruptedException {
        HttpServer server = new HttpServer(8080);
        server.start();
    }
}
